<?php
/**
 *
 *
 *
 */
namespace Yz\modules\Generic;

class Action_SelectNeighbor
    extends Action_Select
    implements \Yz_Action_Interface
{

    /**
     *  @var int ?
     */
    protected $id;
    protected $operation;
    protected $order;

    /**
     *  @param int id > 0
     *  @return this
     */
    public function setId( $id )
    {
        debug();
        $this->id = \Yz_Int::testPositive($id);
        return $this;
    }

    /**
     *  @return this
     */
    public function selectPreviousItem()
    {
        debug();
        $this->operation = '<';
        $this->order = 'DESC';

        return $this;
    }

    /**
     *  @return this
     */
    public function selectNextItem()
    {
        debug();
        $this->operation = '>';
        $this->order = 'ASC';
        return $this;
    }


    /**
     *  Modified SELECT statement creator
     *
     */
    protected function buildSelect()
    {
        $fieldSpec = $this->primaryTable . ".id";
        $sql = parent::buildSelect();
        $sql
            ->where($fieldSpec . $this->operation . ":FetchById_id")
            ->bind("FetchById_id", $this->id)
            ->orderBy($fieldSpec . ' ' . $this->order)
            ->limit(1)
            ;
        return $sql;
    }


}
